// 商品分类
import api from '@/tools/api.js'
import promise from '@/tools/promise'
// import { SUCCESS_STATUS } from '@/config/status'
import {
  RECOMMEND_TREE,
  RECOMMEND_COVER,
  RECOMMEND_DATA,
  RECOMMEND_INFO
} from '@/store/mutation-types'

const state = {
  recommendTree: [],
  cover: {},
  saveData: {},
  recommendInfo: {}
}

// mutations
const mutations = {
  [RECOMMEND_TREE] (state, res) {
    state.recommendTree = res
  },
  [RECOMMEND_COVER] (state, data) {
    state.cover = data
  },
  [RECOMMEND_DATA] (state, res) {
    state.saveData = res
  },
  [RECOMMEND_INFO] (state, res) {
    state.recommendInfo = res
  }
}

// actions
const actions = {
  // 获取推荐位树
  async recommendTree ({ commit }, payload) {
    let treeRes = await api('serverrecommend/recommend/Recommend/getTree', payload)
    commit(RECOMMEND_TREE, treeRes.res)
  },
  // 推荐位删除
  async recommendDel ({ commit }, payload) {
    let delRes = await api('serverrecommend/recommend/Recommend/delOneCate', payload)
    return promise(delRes)
  },
  // 推荐位添加
  async recommendAdd ({ commit }, payload) {
    let addRes = await api('serverrecommend/recommend/Recommend/addRecommend', payload)
    return promise(addRes)
  },
  // 获取推荐位详情
  async recommendInfo ({ commit }, payload) {
    let infoRes = await api('serverrecommend/recommend/Recommend/oneCateInfo', payload)
    commit(RECOMMEND_INFO, infoRes.res)
  },
  // 推荐位编辑
  async recommendEdit ({ commit }, payload) {
    let editRes = await api('serverrecommend/recommend/Recommend/editRecommend', payload)
    return promise(editRes)
  },
  // 商品分类推荐
  async recommendClass ({ commit }, payload) {
    let res = await api('servergoods/goods/GoodsCategory/recommend', payload)
    return promise(res)
  },
  // 商品推荐
  async recommendGoods ({ commit }, payload) {
    let res = await api('servergoods/goods/Goods/recommend', payload)
    return promise(res)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
